/**
 * @(#) WorkMediator.as
 *
 * @date		2009. 02. 25.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	근무 - Mediator
 * 				근무관리 Presentation Logic을 관리 
 */
package com.skt.common_hr.teamprofile.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.teamprofile.TeamProfileFacade;
	import com.skt.common_hr.teamprofile.event.SearchNameEvent;
	import com.skt.common_hr.teamprofile.event.SelDeptEvent;
	import com.skt.common_hr.teamprofile.event.SelMemberEvent;
	import com.skt.common_hr.teamprofile.model.MemberSearchProxy;
	import com.skt.common_hr.teamprofile.model.OrgProxy;
	import com.skt.common_hr.teamprofile.view.components.TeamView;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	

	public class TeamViewMediator extends OverViewMediator
	{
		public static const NAME:String = "TeamViewMediator"; 


		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function TeamViewMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
			var orgProxy:OrgProxy = facade.retrieveProxy(OrgProxy.NAME) as OrgProxy;
			orgProxy.load(Variables.myPernr, "X");
			
			super.initializeView();
 		}
 		
		override protected function activatedView():void
		{
			setEventListeners();
		}
		
		override protected function closedView():void
		{
			removeEventListeners();
		}
		
		private function setEventListeners():void
		{
			view.addEventListener(SelDeptEvent.SELECTED_DEPT, onSelectedDept);
			view.addEventListener(SelMemberEvent.SELECTED_MEMBER, onSelectedMember);
			view.addEventListener(TeamView.EVENT_CHANGE_BASE_STATE, onChangaBaseState);
			view.addEventListener(SearchNameEvent.SEARCH_NAME, onSearchName);
		} 		
		
		private function removeEventListeners():void
		{
			view.removeEventListener(SelDeptEvent.SELECTED_DEPT, onSelectedDept);
			view.removeEventListener(SelMemberEvent.SELECTED_MEMBER, onSelectedMember);
			view.removeEventListener(TeamView.EVENT_CHANGE_BASE_STATE, onChangaBaseState);
			view.removeEventListener(SearchNameEvent.SEARCH_NAME, onSearchName);
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat([
					TeamProfileFacade.LOAD_ORGLIST_COMPLETE,
					TeamProfileFacade.LOAD_ORGLIST_FAILED,
					TeamProfileFacade.LOAD_MEMBERSEARCH_COMPLETE,
					TeamProfileFacade.LOAD_MEMBERSEARCH_FAILED,
					TeamProfileFacade.LOAD_NAMESEARCH_COMPLETE,
					TeamProfileFacade.LOAD_NAMESEARCH_FAILED
					]);
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case TeamProfileFacade.LOAD_ORGLIST_COMPLETE:
					view.orgList = (note.getBody() as OrgProxy).orgList;
//shgoo : 구성원  수  표시  2009.05.28	
					//view.headsLab.text = view.MEMBER_COUNT_HEAD_STRING + view.orgList[0].@heads + view.MEMBER_COUNT_TAIL_STRING;			
/////////////////////////////////
					SelectedDeptCall(Variables.myOrgCode);		//접속자의 소속분원 조회
					view.callLater(view.userDeptfind,[Variables.myOrgCode]);	//접속자의 소속분원  트리 열기
					view.memberFilter();						//섬세한검색 필터 실행
					view.MemberSort();							//섬세한검색 소팅 실행
					//setStatusMessage("데이터가 정상적으로 로드되었습니다."); 
					commonFacade.setStatusMessage("STATUS_SELECT_MEMBER");
					break;
				case TeamProfileFacade.LOAD_ORGLIST_FAILED:
					break;
				case TeamProfileFacade.LOAD_MEMBERSEARCH_COMPLETE:
					var tmpMemberList:ArrayCollection = (note.getBody() as MemberSearchProxy).memberList;
					selectResult(tmpMemberList);
					break;
				case TeamProfileFacade.LOAD_MEMBERSEARCH_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, (note.getBody() as MemberSearchProxy).msgStr);
					break;	
				case TeamProfileFacade.LOAD_NAMESEARCH_COMPLETE:
					var tmpMemberList2:ArrayCollection = (note.getBody() as MemberSearchProxy).memberList;
					selectResult(tmpMemberList2);
					if(tmpMemberList2.length == 0){
						CommonAlert.showMessage(commonFacade.bundle, "ALERT_NO_RESULT_SEARCH");
					}
					break;
				case TeamProfileFacade.LOAD_NAMESEARCH_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, (note.getBody() as MemberSearchProxy).msgStr);
					break;	
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		private function selectResult(memberArr:ArrayCollection):void{
			view.memberList = memberArr;
		 	view.memberFilter(); 
			view.MemberSort();
			//setStatusMessage("데이터가 정상적으로 로드되었습니다.");
		}
		
		


		private function onSelectedDept(event:SelDeptEvent):void
		{
			SelectedDeptCall(event.deptCode);
		}
		
		private function SelectedDeptCall(deptCode:String):void{
			var proxy:MemberSearchProxy = facade.retrieveProxy(MemberSearchProxy.NAME) as MemberSearchProxy;
			proxy.load(deptCode, "");
		}
		
		
		private function onSearchName(event:SearchNameEvent):void
		{
			var proxy:MemberSearchProxy = facade.retrieveProxy(MemberSearchProxy.NAME) as MemberSearchProxy;
			proxy.nameLoad("", event.name);
		}
		private function onSelectedMember(event:SelMemberEvent):void
		{
			if(view.currentState == null || view.currentState == "search_view") {
				view.currentState = "detail_view";
				facade.registerMediator(new ProfileMediator(view.overView));
				view.tileMember.dataProvider = null;
			}
			facade.sendNotification(TeamProfileFacade.CHANGE_SELECTED_MEMBER, event.pernr);
		}

		private function onChangaBaseState(event:Event):void
		{
			facade.removeMediator(ProfileMediator.NAME);
			if(view.oldCurrentMode == "search_view"){
				view.currentState = "search_view";
			}else{
				view.currentState = null;
			}
		}
		

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():TeamView
        {
            return viewComponent as TeamView;
        }
	}
}